clear all
dpath = pwd;

datnames = {'S1','S1trialdata.dat',  'CFL',  12
    'S2','S2trialdata.dat',      'CFL',  9
    'S3','S3trialdata.dat',      'CFL',  3
    'S4','S4trialdata.dat','CFL',  10
    'S5','S5trialdata.dat',      'CFL',  2
    'S6','S6trialdata.dat',   'CFL',  4.5
    'S7','S7trialdata.dat',   'NORM', 3.5
    'S8','S8trialdata.dat',    'CFL',  8
    'S9','S9trialdata.dat',     'NORM', 8
    'S10','S10trialdata.dat', 'CFL',  9
    'S11','S11trialdata.dat',     'NORM', 8
    'S12','NO DATA(subject unable to see targets)',  'NORM', 8
    'S13','NO DATA(subject unable to see targets)',  'CFL', 5
    'S14','S14trialdata.dat',      'NORM', 6
    'S15','NO DATA(subject unable to see targets)',  'CFL',  9
    'S16','S16trialdata.dat',     'NORM', 4
    'S17','S17trialdata.dat',    'CFL',  8
    'S18','S18trialdata.dat',     'NORM', 4
    'S19','S19trialdata.dat',     'NORM', 6};

%%
subdat = [];
for S = 1:size(datnames,1)
    cond = 0;
    if(~(strcmp(datnames{S,2}(1:2),'NO'))) %#ok<*RGXP1>
        fid = fopen([dpath datnames{S,2}],'r');
        
        A = fgetl(fid);
        while(ischar(A))
            A = fgetl(fid);
            if(A>0)
                if(length(regexp(A,'duration')>0)) %#ok<*ISMT>
                    cond = cond+1;
                    duration = sscanf(A,'%*s %f %*s');
                    subdat(S).dat(cond).duration = duration;
                    tcount = 0;
                    subdat(S).dat(cond).dataColumns = {'TrialNum','Target(On/Off)','Response(Yes/No)','Rtime(Sec)'};
                end;
                if(strcmp(A(1),'L'))
                    tcount = tcount+1;
                    dvals = sscanf(A,'%*s %f %f %f %f%*s');%these values are trial ID, target presence/absence (1/0), response yes/no (1/0), and response time (not evaluated in the paper)
                    subdat(S).dat(cond).data(tcount,:) = [dvals'];
                end;
            end;
        end;
        fclose(fid);
    else
        subdat(S).note = 'SUBJECT COULD NOT SEE TARGETS';
    end;
end;
%%
durations = [];
Ntrials = [];

for S = 1:length(subdat)
    for D = 1:length(subdat(S).dat)
        if(~isempty(subdat(S).dat(D).data))%there are a few dummy headings in the data - false starts, need to skip over these
            durations(S,D) = subdat(S).dat(D).duration;
            responses = subdat(S).dat(D).data(:,3);
            targets = subdat(S).dat(D).data(:,2);
            
            validts = responses>-1;%-1 responses are 'skipped' trials, where subject would not response
            Ntrials(S,D) = sum(validts);%number of valid trials
            Nhits(S,D) = sum(responses(targets==1)==1);%total number of 'yes' (1) responses to targets (1s)
            Ntargs(S,D) = sum(targets(validts));%number of valid targets
            NFAs(S,D) = sum(responses(targets==0)==1);%total N false alarms (yes to no target/blank)
            Nblanks(S,D) = sum(targets(validts)==0);
        end;
    end;
end;
%%
%compute sensitivity (d prime) for each subject/duration
udurs = unique(durations);
udurs = udurs(udurs>0);

for S = 1:length(subdat)
    eccs(S) = datnames{S,4};
    subdat(S).uds = unique(durations(S,:));
    subdat(S).uds = subdat(S).uds(subdat(S).uds>0);
    for D = 1:length(subdat(S).uds)
        inds = durations(S,:)==subdat(S).uds(D);
        subdat(S).Ntrials(D) = sum(Ntrials(S,inds));
        subdat(S).Ntargs(D) = sum(Ntargs(S,inds));
        subdat(S).Nblanks(D) = sum(Nblanks(S,inds));
        subdat(S).Nhits(D) = sum(Nhits(S,inds));
        subdat(S).NFAs(D) = sum(NFAs(S,inds));
        
        subdat(S).d_prime(D) = norminv(.01+.98*subdat(S).Nhits(D)./subdat(S).Ntargs(D)) - ...
            norminv(.01+.98*subdat(S).NFAs(D)./subdat(S).Nblanks(D));
        
        all_d_primes(S,udurs==subdat(S).uds(D)) = subdat(S).d_prime(D);
    end;
end;
all_d_primes(all_d_primes==0) = NaN;
%%
%plot data in form similar to Figure 2 in the paper
ecmat = repmat(eccs(:),[1 length(udurs)]);
durs = repmat(udurs,[length(subdat) 1]);
subtypes = strcmp(repmat(datnames(:,3),[1 length(udurs)]),'CFL');

cols = [1 0 0;0 0 1];
figure(1),
scatter(ecmat(subtypes),all_d_primes(subtypes),durs(subtypes),cols(1,:),'s');
hold on
scatter(ecmat(~subtypes),all_d_primes(~subtypes),durs(~subtypes),cols(2,:),'s');
hold off
legend('CFL PATIENTS','CONTROL');
set(gca,'FontSize',16)
xlabel('target eccentricity')
ylabel('d-prime')